package com.hww.acommon.test;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.util.Base64;
import android.util.Log;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.hww.acommon.R;
import com.hww.common.MyAppThis;
import com.hww.common.util.ConvertUtils;
import com.hww.common.util.FileUtils;
import com.hww.common.util.ImageUtils;
import com.hww.common.util.ShellUtils;
import com.hww.common.util.StringUtils;
import com.hww.common.util.ThreadUtils;
import com.hww.common.util.constant.MemoryConstants;

import java.util.concurrent.ExecutorService;

/**
 * @author hww
 * @date 0017 2024/6/17
 * email: heww@zhongyuntech.cn
 * description:
 */
public class ImageActivity extends BaseActivity {

    private static final int REQUEST_CODE = 0x1001;
    private Context context;
    private static final ExecutorService singlePool = ThreadUtils.getSinglePool();

    @Override
    protected void initDate() {
        context = ImageActivity.this;

        ImageView iv1 = new ImageView(ImageActivity.this);
        ImageView iv2 = new ImageView(ImageActivity.this);
        ImageView iv3 = new ImageView(ImageActivity.this);
        ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(300, 300);
        iv1.setLayoutParams(layoutParams);
        iv2.setLayoutParams(layoutParams);
        iv3.setLayoutParams(layoutParams);
        binding.baseLinearLayout.addView(iv1);
        binding.baseLinearLayout.addView(iv2);
        binding.baseLinearLayout.addView(iv3);
        updateLayout();

        setGONE();

        setBTText(binding.bt1, "base64");
        setBTText(binding.bt2, "缩放");
        setBTText(binding.bt3, "剪切指定区域");
        setBTText(binding.bt4, "圆角与灰色");
        setBTText(binding.bt5, "测试Base64");
        setBTText(binding.bt6, "file");

        Drawable drawable = MyAppThis.getThis().getResources().getDrawable(R.drawable.one);
        Bitmap bitmap = ImageUtils.drawable2Bitmap(drawable);
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();

        binding.bt1.setOnClickListener((view) -> {
            System.out.println("base64--------------------");

            ThreadUtils.executeBySingle(task1);
        });
        binding.bt2.setOnClickListener((view) -> {
            singlePool.execute(new Runnable() {
                @Override
                public void run() {

                    Bitmap skew1 = ImageUtils.skew(bitmap, 0.5f, 0f);
                    Bitmap skew2 = ImageUtils.skew(bitmap, 0f, 0.5f);
                    ThreadUtils.runOnUiThread(() -> {
                        iv1.setImageBitmap(bitmap);
                        iv2.setImageBitmap(skew1);
                        iv3.setImageBitmap(skew2);
                    });
                }
            });
        });
        binding.bt3.setOnClickListener((view) -> {
            singlePool.execute(() -> {
                int clipSize = 80;
                Bitmap clip = ImageUtils.clip(bitmap, clipSize, clipSize, width - clipSize, height - clipSize);
                Bitmap scale = ImageUtils.scale(bitmap, 250, 250);
                String format = String.format("old w=%s h=%s    new w=%s h=%s", width, height, scale.getWidth(), scale.getHeight());
                System.out.println(format);
                ThreadUtils.runOnUiThread(() -> {
                    iv1.setImageBitmap(bitmap);
                    iv2.setImageBitmap(clip);
                    iv3.setImageBitmap(scale);
                });
            });
        });

        binding.bt4.setOnClickListener((view) -> {
            singlePool.execute(() -> {
                Bitmap gray = ImageUtils.toGray(bitmap);
                Bitmap toRound = ImageUtils.toRoundCorner(bitmap, width / 3, 15, 0xFFD81B60);
                ThreadUtils.runOnUiThread(() -> {
                    iv1.setImageBitmap(bitmap);
                    iv2.setImageBitmap(toRound);
                    iv3.setImageBitmap(gray);
                });
            });
        });
        binding.bt5.setOnClickListener((view) -> {
            singlePool.execute(() -> {
                /*  Base64.DEFAULT | Base64.NO_WRAP  标准的Base64字符集，同时不包含转换符*/
                String base64Default = ImageUtils.bitmap2Base64(bitmap, Base64.DEFAULT | Base64.NO_WRAP);

                byte[] bytes = ImageUtils.bitmap2Bytes(bitmap);
                String encode = com.hww.common.util.base64.Base64.encode(bytes);
                boolean equals = base64Default.equals(encode);
                System.out.println("equals = " + equals);

                String con = "测试内容123ABC!@#";
                String strA = Base64.encodeToString(StringUtils.bytes(con), Base64.DEFAULT);
                String encodeB = com.hww.common.util.base64.Base64.encode(StringUtils.bytes(con));
                System.out.println("equals = " + encodeB.equals(strA));
            });
        });
        binding.bt6.setOnClickListener((view) -> {
            System.out.println("file--------------------");

            singlePool.execute(() -> {
                try {
                    String path = "/sdcard/DCIM/Camera/";
                    String size = FileUtils.getSize(path);
                    long fsAvailableSize = FileUtils.getFsAvailableSize(path);
                    long fsTotalSize = FileUtils.getFsTotalSize(path);
                    String sizeTotal = ConvertUtils.byte2FitMemorySize(fsAvailableSize);
                    String sizeAvailable = ConvertUtils.byte2FitMemorySize(fsTotalSize);
                    System.out.printf("系统总内存：%s  系统剩余可用总内存：%s%n", sizeTotal, sizeAvailable);
                } catch (Exception e) {
                    e.printStackTrace();
                }

                ShellUtils.CommandResult commandResult = ShellUtils.execCommand("ping -c 10 192.168.10.85", true);
                System.out.println("file--------------------");
            });
        });
    }

    ThreadUtils.Task<Object> task1 = new ThreadUtils.Task<Object>() {
        @Override
        public Object doInBackground() throws Throwable {
            System.out.println("ThreadUtils.Task ==> doInBackground " + Thread.currentThread().toString() + "  \n");

            Resources resources = MyAppThis.getThis().getResources();
            Bitmap bitmap = BitmapFactory.decodeResource(resources, R.drawable.one);
            String s = ImageUtils.bitmap2Base64(bitmap);
            System.out.println("base64 = " + s.length());
            Bitmap base642Bitmap = ImageUtils.base642Bitmap(s);
            byte[] bytes = ImageUtils.bitmap2Bytes(base642Bitmap);
            double v = ConvertUtils.byte2MemorySize(bytes.length, MemoryConstants.KB);
            System.out.println("base642Bitmap = " + v);

            return null;
        }

        @Override
        public void onSuccess(Object result) {
            System.out.println("ThreadUtils.Task ==> onSuccess " + Thread.currentThread().toString() + "  \n" + result);
        }

        @Override
        public void onCancel() {
            System.out.println("ThreadUtils.Task ==> onCancel" + Thread.currentThread().toString() + "  \n");
        }

        @Override
        public void onFail(Throwable t) {
            System.out.println("ThreadUtils.Task ==> onFail" + Thread.currentThread().toString() + "  \n" + Log.getStackTraceString(t));
        }
    };


}
